﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DataService;

public partial class Users_accountAddUser : BasePage
{
    private UserService _userService;
    public Users_accountAddUser()
    {
        _userService = new UserService();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            rptRole.DataSource = _userService.GetRoles(UserLogin);
            rptServerRole.DataSource = _userService.GetServerRoles(UserLogin);
            DataBind();
        }

        //ContactSubmit1.EditUser = UserLogin;

        //Control divContact = ContactSubmit1.FindControl("divContact");
        //if (divContact != null) divContact.Visible = false;

        //Control divBillingType = ContactSubmit1.FindControl("divBillingType");
        //if (divBillingType != null) divBillingType.Visible = false;
    }
    protected void btAddUser_Click(object sender, EventArgs e)
    {
        int CurrentUser= _userService.InsertUpdateUserInfo(UserLogin.Customer.Id, null, txtUserName.Text, txtPasswordNew.Text, txtFirstName.Text, txtLastName.Text, txtEmail.Text, txtPhone.Text);



        foreach (RepeaterItem item in rptRole.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chkRole");
            HiddenField hdn = (HiddenField)item.FindControl("hdnRoleId");
            _userService.InsertUpdateUserRoleAssociation(CurrentUser, Convert.ToInt32(hdn.Value), chk.Checked);
        }
        //Repeater rptServerRole = (Repeater)ContactSubmit1.FindControl("rptServerRole");
        foreach (RepeaterItem item in rptServerRole.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chkServerRole");
            HiddenField hdn = (HiddenField)item.FindControl("hdnRoleId");
            _userService.InsertUpdateUserRoleAssociation(CurrentUser, Convert.ToInt32(hdn.Value), chk.Checked);
        }

        Response.Redirect("Default.aspx");
    }
}